package com.qzsoft.zhjd.cement.modules.cementhomepage.service.impl;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.qzsoft.zhjd.cement.modules.cementhomepage.dto.CementMonthAndYearCountDto;
import com.qzsoft.zhjd.cement.modules.cementhomepage.dto.CementMonthCountDto;
import com.qzsoft.zhjd.cement.modules.cementhomepage.mapper.CementHomePageMapper;
import com.qzsoft.zhjd.cement.modules.cementhomepage.service.CementHomePageService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @ClassName CementHomePageServiceImpl
 * @Description 水稳拌和站首页业务接口实现
 * @author linhuawei
 * @since 2022-01-10
 */
@Service
public class CementHomePageServiceImpl implements CementHomePageService {

    @Autowired
    private CementHomePageMapper cementHomePageMapper;

    @Override
    public BigDecimal countByYear(String baseCode, String year) {
        return cementHomePageMapper.countByYear(baseCode, year);
    }

    @Override
    public List<CementMonthCountDto> countForMonth(String baseCode, String year) {
        return cementHomePageMapper.countForMonth(baseCode, year);
    }

    @Override
    public CementMonthAndYearCountDto getCountByMonthAndYear(String baseCode, String month, String year) {
        Date date = new Date();
        SimpleDateFormat sdf = null;
        if (ObjectUtils.isEmpty(year)) {
            sdf = new SimpleDateFormat("yyyy");
            year = sdf.format(date);
        }
        if (ObjectUtils.isEmpty(month)) {
            sdf = new SimpleDateFormat("yyyy-MM");
            month = sdf.format(date);
        }
        CementMonthAndYearCountDto dto = cementHomePageMapper.getCountByMonthAndYear(baseCode, month, year);
        if (null == dto) {
            dto = new CementMonthAndYearCountDto();
            dto.setMonthWeight(BigDecimal.ZERO);
            dto.setYearWeight(BigDecimal.ZERO);
        }
        dto.setMonth(month);
        dto.setYear(year);
        return dto;
    }

}
